package com.basic.common.utils.easyExcel;

import cn.idev.excel.write.handler.SheetWriteHandler;
import cn.idev.excel.write.metadata.holder.WriteSheetHolder;
import cn.idev.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Excel设置下拉框
 *
 * @author zhaoxy_jn@163.com
 */
public class DropDownWriteHandler implements SheetWriteHandler {

    List<String> dropDown;                  // 下拉框显示的数值
    int colIndex;

    public DropDownWriteHandler(List<String> dropDown, int colIndex) {
        this.dropDown = dropDown;
        this.colIndex = colIndex;
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        // 定义一个map key是需要添加下拉框的列的index value是下拉框数据
        Map<Integer, String[]> mapDropDown = new HashMap<>(3);
        //下拉选项
        String[] downArray = dropDown.toArray(new String[dropDown.size()]);
        //下拉选在Excel中对应的列
        mapDropDown.put(colIndex, downArray);
        //获取一个workbook
        Sheet sheet = writeSheetHolder.getSheet();
        //设置下拉框
        DataValidationHelper helper = sheet.getDataValidationHelper();


        for (Map.Entry<Integer, String[]> entry : mapDropDown.entrySet()) {
            //定义sheet的名称
            String hiddenName = "下拉框数据" + entry.getKey();
            //1.创建一个隐藏的sheet 名称为 hidden
            Workbook workbook = writeWorkbookHolder.getWorkbook();
            Sheet hidden = workbook.createSheet(hiddenName);
            workbook.setSheetHidden(workbook.getSheetIndex(hidden), true);
            //创建一个名称
            Name category1Name = workbook.createName();
            category1Name.setNameName(hiddenName);
            //下拉框的起始行,结束行,起始列,结束列
            CellRangeAddressList addressList = new CellRangeAddressList(1, 2000, entry.getKey(), entry.getKey());
            //获取excel列名
            String excelLine = getExcelLine(entry.getKey());
            //2.循环赋值
            String[] values = entry.getValue();
            for (int i = 0, length = values.length; i < length; i++) {
                // 3:表示你开始的行数  3表示 你开始的列数
                Row row = hidden.getRow(i);
                if (row == null) {
                    row = hidden.createRow(i);
                }
                row.createCell(entry.getKey()).setCellValue(values[i]);
            }
            //4.  =hidden!$H:$1:$H$50  sheet为hidden的 H1列开始H50行数据获取下拉数组
            String refers = "="+hiddenName + "!$"+excelLine+
                    "$1:$"+excelLine +"$"+ (values.length);
            //5 将刚才设置的sheet引用到你的下拉列表中
            DataValidation dataValidation = createDropDownValidation(helper, refers, addressList);
            writeSheetHolder.getSheet().addValidationData(dataValidation);
        }
    }

    /**
     * 创建下拉框数据验证
     * @param helper 数据验证帮助类
     * @param formulaListConstraint 引用的下拉框数据范围
     * @param addressList 下拉框应用的单元格范围
     * @return 数据验证对象
     */
    private DataValidation createDropDownValidation(DataValidationHelper helper, String formulaListConstraint, CellRangeAddressList addressList) {
        DataValidationConstraint constraint = helper.createFormulaListConstraint(formulaListConstraint);
        DataValidation dataValidation = helper.createValidation(constraint, addressList);
        // 阻止输入非下拉选项的值
        dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
        dataValidation.setShowErrorBox(true);
        dataValidation.setSuppressDropDownArrow(true);
        dataValidation.createErrorBox("提示", "请输入下拉选项中的内容");
        return dataValidation;
    }

    /**
     * 返回excel列标A-Z-AA-ZZ
     * @param num 列数
     */
    public static String getExcelLine(int num) {
        String line = "";
        int first = num/26;
        int second = num % 26;
        if (first>0) {
            line = (char)('A'+first-1)+"";
        }
        line += (char)('A'+second)+"";
        return line;
    }


}
